package com.oa.ylms.apps.dao.file.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.dao.file.IFileSubmitInfoDao;
import com.oa.ylms.apps.entity.file.FileSubmitInfoValue;
import com.oa.ylms.apps.entity.organization.SectionInfoValue;

public class FileSubmitInfoDaoImpl extends HibernateDaoSupport implements
		IFileSubmitInfoDao {
	
	private List<FileSubmitInfoValue>list;
	private Integer numbers;
	
	
	@Override
	public int addFileSubmitInfo(FileSubmitInfoValue fileSubmitInfo) {
		if (fileSubmitInfo != null) {
			this.getHibernateTemplate().save(fileSubmitInfo);
			return fileSubmitInfo.getId();
		}
		return 0;
	}

	@Override
	public boolean editFileSubmitInfo(FileSubmitInfoValue fileSubmitInfo) throws Exception {
		if(fileSubmitInfo!=null){
			try{
				this.getHibernateTemplate().update(fileSubmitInfo);
				return true;
			}catch(Exception e){
				return false;
			}
		}
		return false;
	}


	@Override
	public boolean deleteFileSubmitInfo(FileSubmitInfoValue fileSubmitInfo)
			throws Exception {
		if (fileSubmitInfo != null) {
			try {
				this.getHibernateTemplate().delete(fileSubmitInfo);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		return false;
	}

	@Override
	public FileSubmitInfoValue queryFileSubmitInfoById(
			FileSubmitInfoValue fileSubmitInfo)throws Exception {
		if(fileSubmitInfo!=null){
			return (FileSubmitInfoValue) this.getHibernateTemplate().get(FileSubmitInfoValue.class, fileSubmitInfo.getId());
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FileSubmitInfoValue> queryFileSubmitInfoWithPage(final FileSubmitInfoValue fileSubmitInfo, final SectionInfoValue sectionInfo, final int rows, final int pageNo)throws Exception{
		String hqlStr="SELECT fsi FROM FileSubmitInfoValue fsi,StaffInfoValue siv, ArchiveInfoValue aiv, PostInfoValue piv,SectionInfoValue sniv WHERE 1=1 AND fsi.staffInfo.staffId = siv.staffId AND siv.archiveInfo.archiveId = aiv.archiveId AND aiv.postInfo.postId = piv.postId AND piv.sectionInfo.sectionId = sniv.sectionId";
		if(fileSubmitInfo.getStaffInfo() != null){
			hqlStr+=" AND fsi.staffInfo=?";
		}
		if(fileSubmitInfo.getFileInfo()!=null){
			hqlStr+=" AND fsi.fileInfo=?";
		}
		if(fileSubmitInfo.getSubmitTime()!=null){
			hqlStr+=" AND fsi.submitTime <=?";
		}
		if(sectionInfo != null && sectionInfo.getSectionId()!=null){
			hqlStr+=" AND sniv.sectionId=?";
		}
		hqlStr+=" ORDER BY fsi.submitTime DESC";
		final String hql=hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if(fileSubmitInfo.getStaffInfo() != null){
					query.setEntity(++index, fileSubmitInfo.getStaffInfo());
				}
				if(fileSubmitInfo.getFileInfo() != null){
					query.setEntity(++index, fileSubmitInfo.getFileInfo());
				}
				if(fileSubmitInfo.getSubmitTime()!=null){
					query.setDate(++index, fileSubmitInfo.getSubmitTime());
				}
				if(sectionInfo != null && sectionInfo.getSectionId()!=null){
					query.setInteger(++index, sectionInfo.getSectionId());
				}
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				list = query.list();
				return list;
			}
		});
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public int countTPages(final FileSubmitInfoValue fileSubmitInfo, final SectionInfoValue sectionInfo, final int rows) throws Exception {
			int pageNumbers = 0;
			String hqlStr="SELECT fsi FROM FileSubmitInfoValue fsi,StaffInfoValue siv, ArchiveInfoValue aiv, PostInfoValue piv,SectionInfoValue sniv WHERE 1=1 AND fsi.staffInfo.staffId = siv.staffId AND siv.archiveInfo.archiveId = aiv.archiveId AND aiv.postInfo.postId = piv.postId AND piv.sectionInfo.sectionId = sniv.sectionId";
			if(fileSubmitInfo.getStaffInfo() != null){
				hqlStr+=" AND fsi.staffInfo=?";
			}
			if(fileSubmitInfo.getFileInfo()!=null){
				hqlStr+=" AND fsi.fileInfo=?";
			}
			if(fileSubmitInfo.getSubmitTime()!=null){
				hqlStr+=" AND fsi.submitTime <=?";
			}
			if(sectionInfo != null && sectionInfo.getSectionId()!=null){
				hqlStr+=" AND sniv.sectionId=?";
			}
			hqlStr+=" ORDER BY fsi.submitTime DESC";
			final String hql=hqlStr;
			list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery(hql);
					int index = -1;
					if(fileSubmitInfo.getStaffInfo() != null){
						query.setEntity(++index, fileSubmitInfo.getStaffInfo());
					}
					if(fileSubmitInfo.getFileInfo() != null){
						query.setEntity(++index, fileSubmitInfo.getFileInfo());
					}
					if(fileSubmitInfo.getSubmitTime()!=null){
						query.setDate(++index, fileSubmitInfo.getSubmitTime());
					}
					if(sectionInfo != null && sectionInfo.getSectionId()!=null){
						query.setInteger(++index, sectionInfo.getSectionId());
					}
					list = query.list();
					return list;
				}
			});
			numbers = list.size();
			if (numbers <= rows) {
				pageNumbers = 1;
			} else {
				if (numbers % rows == 0) {
					pageNumbers = numbers / rows;
				} else {
					pageNumbers = numbers / rows + 1;
				}
			}
		return pageNumbers;
	}
	
	@Override
	public int getTotalNumbers() throws Exception{
		return getNumbers();
	}
	
	//
	public List<FileSubmitInfoValue> getList() {
		return list;
	}

	public void setList(List<FileSubmitInfoValue> list) {
		this.list = list;
	}

	public Integer getNumbers() {
		return numbers;
	}

	public void setNumbers(Integer numbers) {
		this.numbers = numbers;
	}

}
